package com.example.myapplication;

import static androidx.test.core.app.ApplicationProvider.getApplicationContext;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

import androidx.test.platform.app.InstrumentationRegistry;
import androidx.test.ext.junit.runners.AndroidJUnit4;

import org.junit.Test;
import org.junit.runner.RunWith;

import static org.junit.Assert.*;

import java.io.File;

/**
 * Instrumented test, which will execute on an Android device.
 *
 * @see <a href="http://d.android.com/tools/testing">Testing documentation</a>
 */
@RunWith(AndroidJUnit4.class)
public class ExampleInstrumentedTest {
    @Test
    public void useAppContext() {
        // Context of the app under test.
        Context appContext = InstrumentationRegistry.getInstrumentation().getTargetContext();
        assertEquals("com.example.myapplication", appContext.getPackageName());

        String TAG = "MainActivity";
        String DATABASE_NAME = "address_book.db";
        // 获取数据库文件路径
        Context context = getApplicationContext();
        String databasePath = context.getDatabasePath(DATABASE_NAME).getPath();

        // 关闭数据库连接
        SQLiteDatabase database = SQLiteDatabase.openDatabase(databasePath, null, SQLiteDatabase.OPEN_READWRITE);
        database.close();

        // 删除数据库文件
        File databaseFile = new File(databasePath);
        if (databaseFile.exists()) {
            boolean deleted = databaseFile.delete();
            if (deleted) {
                Log.d(TAG, "数据库文件已成功删除");
            } else {
                Log.d(TAG, "数据库文件删除失败");
            }
        } else {
            Log.d(TAG, "数据库文件不存在");
        }
    }
}